package com.linkage.service.commodity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.linkage.pojo.commodity.Book;
import com.linkage.repository.commodity.BookRepository;
@Service
public class BookService {
    @Autowired
	private BookRepository bookRepository;
	
	//查看图书是否是下架商品
	public boolean isSoldOut(int bookId){
		Book book=this.bookRepository.isAvailable(bookId);
		return book.getIsAvailable();
	}
	//列出相应类目的图书 ,因为再sql语句中判定了是否上架
	public List<Book> listCategoryBooks(int categoryId){
	    List<Book> list=this.bookRepository.listBooks(categoryId);
	    return list;
	}
	//列出所有可见图书
	public List<Book> listAllAvailableBook(){
	    List<Book> list=this.bookRepository.allBooks();
	    /*for(Book book:list){
            if(book.getIsAvailable()==false){
                list.remove(book);
            }
        }*/
//	    int begin=(pageCurrent-1)*pageCount;
//	    int end=begin+pageCount;
//	    list=list.subList(begin, end);
	    return list;
	}
	public List<Book> listAvailableBook(String bookName){
	    return this.bookRepository.searchBookName("%"+bookName+"%");
	}
	//查询指定的图书信息      过时
	public Book listSelectedBook(int bookid){
	   return this.bookRepository.select(bookid);
	}
	//查询指定的级联了目录表和特价表的图书信息
	public List<Map<String,Object>> listSelectedBook1(int bookId){
	    return this.bookRepository.selectBook(bookId);
	}
	
	//搜索    根据关键字来搜索图书
	//优先级是书名、然后是类目，最后才是模糊搜索
	public List<Book> search(Integer categoryId,String bookName){
	    List<Book> list=new ArrayList<>();
	    if(categoryId!=null && bookName!=null){
	        list=this.bookRepository.searchBookNameAndCategoryId("%"+bookName+"%", categoryId);
	    }
	    /*List<Book> list2=this.bookRepository.searchBookName("%"+bookName+"%");
	    list.addAll(list2);
	    List<Book> list3=this.bookRepository.listBooks(categoryId);
	    list.addAll(list3);*/
	    return list;
	}
	
	/*public List<Book> listBooksPaging(Page page){
	    //TODO
	    return null;
	}*/
}
